package logic

import (
	"campus-trade/models/mysql"
	"context"

	"campus-trade/campusrpc/campusrpc"
	"campus-trade/campusrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CommentListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCommentListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CommentListLogic {
	return &CommentListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CommentListLogic) CommentList(in *campusrpc.CommentListRequest) (*campusrpc.CommentListResponse, error) {
	// todo: add your logic here and delete this line

	var cd []mysql.CommentData
	mysql.DB.Debug().Model(&mysql.Comment{}).
		Select("comments.*,users.id as user_id,books.id as book_id").
		Joins("left join users on users.id = comments.user_id").
		Joins("left join books on books.id = comments.book_id").
		Find(&cd)

	var cache []*campusrpc.CommentList

	for _, i2 := range cd {
		list := campusrpc.CommentList{
			UserId: i2.UserId,
			BookId: i2.BookId,
		}
		cache = append(cache, &list)
	}

	return &campusrpc.CommentListResponse{
		List: cache,
	}, nil
}
